package com.google.code.gronono.commons.observable;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 * Implémentation abstraite de {@link IObservable}.
 * Rajoute les méthodes {@link #firePropertyChange(String, Object, Object)} et {@link #fireIndexedPropertyChange(String, int, Object, Object)}.
 *
 * @author Arnaud BRUNET
 *
 * @see PropertyChangeSupport
 */
public abstract class AbstractObservable implements IObservable {
	/** Le {@link PropertyChangeSupport} pour simplifier l'implémentation. */
	private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

	/*
	 * (non-Javadoc)
	 * @see com.google.code.gronono.commons.observable.IObservable#addPropertyChangeListener(java.beans.PropertyChangeListener)
	 */
	public void addPropertyChangeListener(final PropertyChangeListener listener) {
		pcs.addPropertyChangeListener(listener);
	}

	/*
	 * (non-Javadoc)
	 * @see com.google.code.gronono.commons.observable.IObservable#addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
	 */
	public void addPropertyChangeListener(final String propertyName, final PropertyChangeListener listener) {
		pcs.addPropertyChangeListener(propertyName, listener);
	}

	/**
	 * @see PropertyChangeSupport#fireIndexedPropertyChange(String, int, Object, Object)
	 */
	public void fireIndexedPropertyChange(final String propertyName, final int index, final Object oldValue, final Object newValue) {
		pcs.fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
	}

	/**
	 * @see PropertyChangeSupport#fireIndexedPropertyChange(String, int, Object, Object)
	 */
	public void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) {
		pcs.firePropertyChange(propertyName, oldValue, newValue);
	}

	/*
	 * (non-Javadoc)
	 * @see com.google.code.gronono.commons.observable.IObservable#getPropertyChangeListeners()
	 */
	public PropertyChangeListener[] getPropertyChangeListeners() {
		return pcs.getPropertyChangeListeners();
	}

	/*
	 * (non-Javadoc)
	 * @see com.google.code.gronono.commons.observable.IObservable#getPropertyChangeListeners(java.lang.String)
	 */
	public PropertyChangeListener[] getPropertyChangeListeners(final String propertyName) {
		return pcs.getPropertyChangeListeners(propertyName);
	}

	/*
	 * (non-Javadoc)
	 * @see com.google.code.gronono.commons.observable.IObservable#hasListeners(java.lang.String)
	 */
	public boolean hasListeners(final String propertyName) {
		return pcs.hasListeners(propertyName);
	}

	/*
	 * (non-Javadoc)
	 * @see com.google.code.gronono.commons.observable.IObservable#removePropertyChangeListener(java.beans.PropertyChangeListener)
	 */
	public void removePropertyChangeListener(final PropertyChangeListener listener) {
		pcs.removePropertyChangeListener(listener);
	}

	/*
	 * (non-Javadoc)
	 * @see com.google.code.gronono.commons.observable.IObservable#removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
	 */
	public void removePropertyChangeListener(final String propertyName, final PropertyChangeListener listener) {
		pcs.removePropertyChangeListener(propertyName, listener);
	}

}
